﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;
using Reporter.Data;
using ReporterControlLibrary.Models;
using ReporterSystem;

namespace Reporter.Controls
{
    /// <summary>
    /// Interaction logic for ImportProvisionNotebookDialogBox.xaml
    /// </summary>
    public partial class PublishProjectDialogBox : Window
    {
        public Project Project { get; set; }

        public PublishProjectDialogBox()
        {
            this.InitializeComponent();

            // Insert code required on object creation below this point.
            Loaded += new RoutedEventHandler(PublishProjectDialogBoxLoaded);
        }

        void PublishProjectDialogBoxLoaded(object sender, RoutedEventArgs e)
        {           

            ThreadPool.QueueUserWorkItem(new WaitCallback(
                                             (object o) =>
                                             {
                                                 var status = new PublishStatus();


                                                 status.ChangeStatus += (source, args) => this.Dispatcher.Invoke(new Action(
                                                                                                                     () =>
                                                                                                                     {
                                                                                                                         notebooksPublished_chb.Content = String.Format("Publishing templates ({0} of {1})", status.PublishedNotebooks, status.TotalNotebooks);
                                                                                                                         imagesPublished_chb.Content = String.Format("Publishing images ({0} of {1})", status.PublishedImages, status.TotalImages);

                                                                                                                         if(status.CifPublished)
                                                                                                                             modelPublished_chb.IsChecked = true;

                                                                                                                         if (status.PublishedImages == status.TotalImages)
                                                                                                                             imagesPublished_chb.IsChecked = true;

                                                                                                                         if (status.PublishedNotebooks == status.TotalNotebooks)
                                                                                                                             notebooksPublished_chb.IsChecked = true;

                                                                                                                         if (status.Done)
                                                                                                                         {
                                                                                                                             publishProgressText.Visibility = Visibility.Collapsed;
                                                                                                                             publishFinishedText.Visibility = Visibility.Visible;
                                                                                                                             DoneButton.IsEnabled = true;
                                                                                                                         }
                                                                                                                     }
                                                                                                                     ), null);


                                                 ReportApp.PublishProject(Project.ReportProject, status);
                                             }));

        }

        private void DoneBtClick(object sender, RoutedEventArgs e)
        {
            // form validation comes here

            this.DialogResult = true;
        }
    }
}